package com.example.ymm.modules1.service.impl.sys.iot;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.modules1.entity.my.sys.iot.IotShelf;
import com.example.ymm.modules1.entity.my.sys.iot.IotTag;
import com.example.ymm.modules1.mapper.my.sys.IotTagMapper;
import com.example.ymm.modules1.query.sys.iot.IotTagQueryCriteria;
import com.example.ymm.modules1.query.sys.iot.ViewStorePositionQueryCriteria;
import com.example.ymm.modules1.service.dd.iot.IotHttpUtil;
import com.example.ymm.modules1.service.dto.heng.iot.ViewStorePositionDto;
import com.example.ymm.modules1.service.sys.iot.IotShelfService;
import com.example.ymm.modules1.service.sys.iot.IotTagService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
@DS("slave2")
@Slf4j
public class IotTagServiceImpl extends BaseServiceImpl<IotTagMapper, IotTag> implements IotTagService {

    @Autowired
    IotHttpUtil iotHttpUtil;
    @Autowired
    IotShelfService iotShelfService;

    public List<IotTag> queryAll(IotTagQueryCriteria criteria) {
        List<IotTag> result =baseMapper.selectList(QueryHelpPlus.getPredicate(IotTag.class, criteria));
        return result;
    }

    @Override
    public void setTagLocation(IotTag iotTag) {
        saveOrUpdate(iotTag);
    }

    @Override
    public List<String> bgTagImport(List<JSONObject> list) {
        /**
         * houseStoreName      匹配ms的仓库名称
         * houseLocationName   匹配ms的库位名称
         */
        System.out.println("--bgTagImport:"+list);
        List<String> error=new ArrayList<>();
        if(UtilValidate.isEmpty(list)){
            throw new YmException("导入数据不能为空!");
        }
        for (int i = 0; i < list.size(); i++) {
            JSONObject jsonObject = list.get(i);
            String houseLocationName= jsonObject.getString("houseLocationName");
            String houseStoreName= jsonObject.getString("houseStoreName");
            String shelfName=jsonObject.getString("shelfName");//架子
            String tagNo = jsonObject.getString("tagNo");
            String tagMac = jsonObject.getString("tagMac");
            if(UtilValidate.isEmpty(tagNo)){
                error.add("第"+(i+1)+"行 标签编号不能为空");
            }
            if(UtilValidate.isEmpty(shelfName)){
                error.add("第"+(i+1)+"行 架子名称不能为空");
            }
            IotShelf iotShelf = iotShelfService.getShelfName(shelfName);
            if(UtilValidate.isEmpty(shelfName)){
                error.add("第"+(i+1)+"行 <"+shelfName+"> 匹配不到架子id");
            }
            jsonObject.put("shelfId",iotShelf.getShelfId());
            ViewStorePositionDto oneStorePosition = getOneStorePosition(houseLocationName, houseStoreName);
            if(UtilValidate.isEmpty(oneStorePosition)){
                error.add("第"+(i+1)+"行 ：<"+houseLocationName+":"+houseStoreName+">定位不到ms库位信息!");
            }
            jsonObject.put("houseLocationId", oneStorePosition.getHouseLocationId());
        }
        if(UtilValidate.isNotEmpty(error)){
            return error;
        }
        for (int i = 0; i < list.size(); i++) {
            JSONObject jsonObject = list.get(i);
            String houseLocationName = jsonObject.getString("houseLocationName");
            String houseStoreName = jsonObject.getString("houseStoreName");
            String tagNo = jsonObject.getString("tagNo");
            String tagMac = jsonObject.getString("tagMac");
            IotTag tag = getById(tagNo);
//            if(UtilValidate.isEmpty(tag)){
//                //新增
//                JSONObject j1 = iotHttpUtil.httpPost("/iot/easyfinder/bgTag", jsonObject);
//            }else{
//                //修改
//                JSONObject j1 = iotHttpUtil.httpPut("/iot/easyfinder/bgTag", jsonObject);
//            }
            IotTag iotTag = JSONObject.parseObject(jsonObject.toJSONString(), IotTag.class);
            System.out.println("---iotTag:"+iotTag);
//            saveOrUpdate(iotTag);

        }
        return error;

    }

    @Override
    public List<ViewStorePositionDto> getStorePosition(ViewStorePositionQueryCriteria criteria) {
        return baseMapper.getStorePosition(criteria);
    }

    @Override
    public List<IotTag> findXiandaoDengByTagMacIn(List<String> tagMacIn) {
        IotTagQueryCriteria criteria=new IotTagQueryCriteria();
        criteria.setTagMacIn(tagMacIn);
        return baseMapper.findXiandaoDengByTagMacIn(criteria);
    }

    public ViewStorePositionDto getOneStorePosition(String houseLocationName, String houseStoreName) {
        ViewStorePositionQueryCriteria criteria=new ViewStorePositionQueryCriteria();
        criteria.setHouseLocationName(houseLocationName);
        criteria.setHouseStoreName(houseStoreName);
        List<ViewStorePositionDto> dtos=getStorePosition(criteria);
        if(dtos.size()>0){
            return dtos.get(0);
        }
        return null;
    }

}
